'use strict';

initAssistantPage();
initUploadImg();
function initAssistantPage() {
    var shopList = new Vue({
        el: '#detail-content',
        data: {
            shopList: [],
            'urlHeadCommon': 'http://kmb.leehq.com/pc/',
            'issuePage': false,
            'searchKey': '',
            'pageNum': 0,
            'pageIndex': 0,
            'loading': true,

            'equipment': '',
            'purchaseDate': '',
            'price': '',
            'number': '',
            'imgSrc': '',
            'relativeName': '',
            'phoneNum': '',
            'remindInfo': '',
            'remindInfo1': '',
            'isLogin': false,
            'cardListId': [],
            'otherInfo': '',
            'selectId': '',
            'imgArr': []

        },
        components: {},
        created: function created() {
            if (sessionStorage.getItem('token')) {
                this.isLogin = true;
            }
            this.shopListFun(0, '');
            this.cardListFun();
        },
        mounted: function mounted() {},

        methods: {
            cardListFun: function cardListFun() {
                var _this = this;

                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'p/cardlist' + '?token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                    //beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');}
                });
                ajax.done(function (data) {
                    _this.cardListId = data.data.cardList;
                });
            },
            shopListFun: function shopListFun(offset, queryStr) {
                var _this2 = this;

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'p/good/list?limit=20&offset=' + offset + '&queryStr=' + queryStr,
                    type: 'GET',
                    header: sessionStorage.getItem('token') || ''
                });
                ajax.done(function (data) {
                    if (data.data.blist.datalist) {
                        data.data.blist.datalist.forEach(function (item) {
                            Vue.set(item, 'pictureUrl', { 'background-image': 'url(http://kmb.leehq.com' + item.picUrl + ')' });
                            //this.imgArr.push({'background-image':'url(http://101.132.112.211' + item.picUrl + ')'});
                        });
                        _this2.shopList = data.data.blist.datalist;
                        console.log(_this2.shopList);
                        _this2.pageNum = Math.ceil(data.data.blist.tsize / 20);
                    } else {
                        _this2.shopList = [];
                    }
                    $('.loading').hide();
                    $('#detail-content').show();
                });
            },
            postForm: function postForm(data) {
                var _this3 = this;

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'c/good/add',
                    type: 'POST',
                    data: data
                });
                ajax.done(function () {
                    _this3.shopListFun(0, '');
                    _this3.issuePage = false;
                    _this3.remindInfo = '发布成功';
                    $('.remind-info').show();
                    var t = setTimeout(function () {
                        $('.remind-info').hide();
                    }, 1000);
                });
            },
            showIssuePage: function showIssuePage() {
                if (this.isLogin) {
                    this.issuePage = true;
                } else {
                    $('.login-page').show();
                }
            },
            closeIssuePage: function closeIssuePage(e) {
                if (e.target.classList[0] == 'issue-page' || e.target.classList[0] == 'close-icon') {
                    this.issuePage = false;
                }
            },
            formSure: function formSure() {
                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';
                if (this.equipment) {
                    if (this.purchaseDate) {
                        var purchaseDateArr = this.purchaseDate.split('-');
                        var dateLen = purchaseDateArr.length;

                        if (dateLen === 3 && purchaseDateArr[1].length == 2 && purchaseDateArr[2].length == 2) {
                            if (parseInt(purchaseDateArr[0]) >= 1900 && parseInt(purchaseDateArr[0]) <= 2400) {
                                if (parseInt(purchaseDateArr[1]) >= 1 && parseInt(purchaseDateArr[1]) <= 12) {
                                    if (parseInt(purchaseDateArr[2]) >= 1 && parseInt(purchaseDateArr[2]) <= 31) {
                                        if (this.price) {
                                            if (this.number) {
                                                if (this.imgSrc) {
                                                    if (this.relativeName) {
                                                        if (this.phoneNum) {
                                                            if (!/^1[34578]\d{9}$/.test(this.phoneNum)) {
                                                                $('.remind-phone').show();
                                                            } else {
                                                                $('.remind-style').hide();
                                                                if (this.selectId) {
                                                                    if (this.otherInfo) {
                                                                        var data = {
                                                                            goodName: this.equipment,
                                                                            price: this.price,
                                                                            num: this.number,
                                                                            picUrl: this.imgSrc,
                                                                            linkTel: this.phoneNum,
                                                                            linkName: this.relativeName,
                                                                            buyDateStr: this.purchaseDate,
                                                                            remarks: this.otherInfo,
                                                                            cardTypeId: this.selectId,
                                                                            token: token,
                                                                            telNo: telNo
                                                                        };
                                                                        this.postForm(data);
                                                                    } else {
                                                                        $('.remind-style').hide();
                                                                        $('.remind-other').show();
                                                                    }
                                                                } else {
                                                                    $('.remind-style').hide();
                                                                    $('.remind-cardType').show();
                                                                }
                                                            }
                                                        } else {
                                                            $('.remind-style').hide();
                                                            $('.remind-phone').show();
                                                        }
                                                    } else {
                                                        $('.remind-style').hide();
                                                        $('.remind-person').show();
                                                    }
                                                } else {
                                                    $('.remind-style').hide();
                                                    $('.remind-img').show();
                                                }
                                            } else {
                                                $('.remind-style').hide();
                                                $('.remind-num').show();
                                            }
                                        } else {
                                            $('.remind-style').hide();
                                            $('.remind-price').show();
                                        }
                                    } else {
                                        $('.remind-style').hide();
                                        $('.remind-date').show();
                                    }
                                } else {
                                    $('.remind-style').hide();
                                    $('.remind-date').show();
                                }
                            } else {
                                $('.remind-style').hide();
                                $('.remind-date').show();
                            }
                        } else {
                            $('.remind-style').hide();
                            $('.remind-date').show();
                        }
                    } else {
                        $('.remind-style').hide();
                        $('.remind-date').show();
                    }
                } else {
                    $('.remind-equipment').show();
                }
            },
            formCancel: function formCancel() {
                this.issuePage = false;
            },
            showDetailPage: function showDetailPage(index) {
                $('.shop-detail-page').eq(index).show();
            },
            closeDetailPage: function closeDetailPage(e) {
                if (e.target.classList[0] == 'shop-detail-page') {
                    $('.shop-detail-page').hide();
                }
            },

            clickCallback: function clickCallback(pageNum) {
                console.log(pageNum);
                this.pageIndex = pageNum;
                this.shopListFun((pageNum - 1) * 20, '');
            },
            updateDate: function updateDate(date) {
                this.purchaseDate = date;
            }

        }
    });
    //支付
    $('.tab-btn4').on('click', function () {
        $('.pay-page').show();
    });
    $('.pay-internet,.pay-munu ').on('click', function () {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
    $('.tab-btn2').addClass('active').siblings('.tab-btn').removeClass('active');

    $('.page-style').on('click', function () {
        $(this).addClass('page-size').siblings('.page-style').removeClass('page-size');
    });
    $('.shop-content-list').on('click', function () {
        var index = $(this).eq();
        $('.shop-detail-page')[index].show();
    });
}
function initUploadImg() {
    $('#uploadImg').change(function () {
        $.ajaxFileUpload({
            'url': '/pc/p/img/upload',
            type: 'POST',
            secureuri: false,
            fileElementId: 'uploadImg',
            dataType: 'json',
            success: function success(data, status) {
                console.log(data);
                $('#uploadImgSrc').val(data.data)[0].dispatchEvent(new Event('input'));
            }
        });
    });
}

$('.close-icon').on('click', function () {
    $('.remind-info').hide();
});